import os
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

# ThreadPoolExecutor --> 线程池
# ProcessPoolExecutor --> 进程池

# 默认进程数量 = 当前cpu个数
pool = ProcessPoolExecutor(3)  # 设置固定3个进程


def task(name):
    print(f"{name}出生了 {os.getpid()}")
    time.sleep(2)
    print(f"{name}长大了5岁")
    return "kk" + str(name)


def call_back(res):
    print("call_back:", res.result())


if __name__ == '__main__':
    for i in range(1, 12):
        # 往进程池提交任务 --> 异步提交
        pool.submit(task, i).add_done_callback(call_back)  # 根据进程池大小同时提交任务个数，如本次案例中3个进程为一组提交
        # .add_done_callback(call_back) 异步回调函数

    print("我是主线程")

